﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QueueUnlimited.API;
using System.Diagnostics;
using System.Transactions;

namespace MultiplayerDice.Implementation.Processor
{
    public static class ChooseOpponentProcessor
    {
        public static void Process(GenericEvent genericEvent)
        {
            try
            {
                if (genericEvent.Name == typeof(BusinessEvent.NewPlayerRegisteredEvent).FullName)
                {
                    var businessEvent = genericEvent.Deserialize<BusinessEvent.NewPlayerRegisteredEvent>();

                    using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                    using (var database = new QueryDataStoreContext())
                    {
                        if (0 == (from x in database.Query_ChooseOpponents
                                  where x.Name == businessEvent.PlayerName
                                  select x).Count())
                        {
                            database.Query_ChooseOpponents.InsertOnSubmit(
                                new Query_ChooseOpponent()
                                {
                                    Name = businessEvent.PlayerName
                                });
                            database.SubmitChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
